package io.renren.common.eth.web3j;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Bool;
import org.web3j.abi.datatypes.StaticArray;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.utils.Convert;


import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author lishuai 
 * @qq  314397644
 */
public class EthUtilTest {

    private static Logger log = LoggerFactory.getLogger(EthUtilTest.class);
   // private static String mainAccount = "0x5a62f07785a8255e6c12db988c0361e237d49364";
    private static final  String contractAddress = "0x65623daf4c2d623982e19da7d9552bda5e1655c9";
    private static String password = "12345678A";

  
    /** 创建新的账户
     * @return
     * @throws Exception
     */
    public static boolean testNewAccount(String password) throws Exception {
        boolean newAccount = EthUtil.newAccount(password);
		return newAccount;
    }

  
    /** 获取eth 账户余额
     * @param mainAccount
     * @return
     * @throws Exception
     */
    public static BigInteger testGetAccountBalance(String mainAccount) throws Exception {
		return  EthUtil.getBalance(mainAccount);
    }

    
    /** 查询账户列表
     * @return
     * @throws Exception
     */
    public static List<String>  testAccounts() throws Exception {
        return EthUtil.getAccounts();
    }


    /**查token余额 
     * 参数被查询的钱包地址
     * @throws Exception
     */
   
    public static BigDecimal testGetTokenBalance(String mainAccount) throws Exception {
        String functionName = "balanceOf";
        List<Type> inputParams = Arrays.asList((Type)new Address(mainAccount));
        List<TypeReference<?>> outParams = new ArrayList<>(1);
        outParams.add((TypeReference) new TypeReference<Uint256>() {});
         //查询者地址  合约地址 方法名称 查询地址  输出格式
        List<Type> call = EthUtil.call(mainAccount, contractAddress, functionName, inputParams,outParams);
        //log.info("call result==>{}", JsonUtils.objectToJson(call));
       //  BigInteger decimal = Numeric.decodeQuantity(call.get(0).getValue().toString());
        return Convert.fromWei(call.get(0).getValue().toString(), Convert.Unit.ETHER);
        
    }        
        /**
         * 转账
         *
         * @param from
         * @param to
         * @param amount
         * @return String  transactionsHash
         * @throws Exception
         */
        public String tokenTransferFrom(String from, String to, String password, BigDecimal amount) throws Exception {
            try {
            	BigInteger transferAmount = Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger();
            	log.info("transferAmount----->{}"+transferAmount);
                String result = sendTransaction(from, contractAddress, password, "_transferFrom",
                        new Address(to), new Uint256(transferAmount));
                return result;
            } catch (Exception e) {
                throw new Exception(e);
            }
        }

        
        
        

	/**  发送合约
	 * @param from
	 * @param password
	 * @param contractAddress
	 * @param functionName
	 * @param inputParams
	 * @return
	 * @throws Exception
	 */
	public String sendTransaction(String from, String password, String contractAddress, String functionName, Type... inputParams) throws Exception {
	    try {
	    	 List<TypeReference<?>> outParams = new ArrayList<>(1);
	         outParams.add((TypeReference)new TypeReference<Type>() {});
	        String result = EthUtil.sendTransaction(from, contractAddress, password, functionName,
	                Arrays.asList(inputParams), outParams);
	        log.info("sendTransaction==>{}", result);
	        return result;
	    } catch (Exception e) {
	        throw new Exception(e);
	    }
	}
 
}
